package com.siss.tenancy.elastic.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class RedisConfig  {

		@Value("${spring.redis.database}")
	    private int database;
	
		@Value("${spring.redis.hostName}")
	    private String hostName;
	    
	    @Value("${spring.redis.port}")
	    private int port;
	    
	    @Value("${spring.redis.password}")
	    private String password;
	    
	    @Value("${spring.redis.timeout}")
	    private int timeout;
	    
	    @Value("${spring.redis.pool.maxIdle}")
	    private int maxIdle;
	    
	    @Value("${spring.redis.pool.minIdle}")
	    private int minIdle;
	     
	    
	    @Value("${spring.redis.pool.maxTotal}")
	    private int maxTotal;
	    
	    @Value("${spring.redis.pool.maxWaitMillis}")
	    private long maxWaitMillis;
	    
	    @Value("${spring.redis.pool.testOnBorrow}")
	    private boolean testOnBorrow;
	    
	    public JedisPoolConfig poolCofig(int maxIdle, int maxTotal,  
	            long maxWaitMillis, boolean testOnBorrow) {  
	        JedisPoolConfig poolCofig = new JedisPoolConfig();  
	        poolCofig.setMaxIdle(maxIdle);
	        poolCofig.setMaxIdle(minIdle);
	        poolCofig.setMaxTotal(maxTotal);  
	        poolCofig.setMaxWaitMillis(maxWaitMillis);  
	        poolCofig.setTestOnBorrow(testOnBorrow);  
	        return poolCofig;  
	    }  
	
	    @Bean(name = "redisConnectionFactoryOnDB0")
	   public JedisConnectionFactory redisConnectionFactoryOnDB0() {  
	       JedisConnectionFactory factory = new JedisConnectionFactory();
	       factory.setDatabase(database);
	       factory.setHostName(hostName);  
	       factory.setPort(port);  
	       factory.setPassword(password); 
	       factory.setTimeout(timeout);
	       
	       factory.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis,  testOnBorrow));  
	    		           // 初始化连接pool  
	       factory.afterPropertiesSet();  

	       return factory;  
	   }  
	  

	    @Bean(name = "redisTemplateOnDB0")
	    public RedisTemplate redisTemplateOnDB0(
	    		@Qualifier("redisConnectionFactoryOnDB0")
	            RedisConnectionFactory factory) {
	        StringRedisTemplate template = new StringRedisTemplate(factory);
	        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
	        ObjectMapper om = new ObjectMapper();
	        //om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
	        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
	        jackson2JsonRedisSerializer.setObjectMapper(om);
	        template.setValueSerializer(jackson2JsonRedisSerializer);
	        template.afterPropertiesSet();
	        return template;
	        
      
	    }
	    
     
	    
}
